package  com.macllvm.whale.service;

import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.util.Log;
import com.macllvm.whale.net.*;
import com.macllvm.whale.protocol.*;

public class NetworkService extends Service {

    private static final String TAG = "NetworkService";
    private WifiStateChangedReceiver receiver;
    private ProtocolController controller;
    private UDPServer udpServer;
    private TCPServer tcpServer;
    private UDPClient udpClient;
    private Context context;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();

        context = getApplication().getBaseContext();
        receiver = new WifiStateChangedReceiver();
        IntentFilter filter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        context.registerReceiver(receiver, filter);

        try {
            udpClient = new UDPClient();
            controller = ProtocolController.getInstance();
            controller.setUDPClient(udpClient);
            udpServer = new UDPServer(controller);
            udpServer.start();

            tcpServer = new TCPServer(controller);
            tcpServer.start();

            udpClient.start();

        }catch(Exception e) {
            Log.e(TAG, "network start error..........");
            e.printStackTrace();
        }

        controller.broadcastOnline();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        controller.getTransferInManager().stopAllTask();
        controller.getTransferOutManager().stopAllTask();
        udpServer.stopServer();
        tcpServer.stopServer();
        udpClient.close();
        context.unregisterReceiver(receiver);
    }


    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }


    class WifiStateChangedReceiver extends BroadcastReceiver {
        private static final String TAG = "WifiStateChangedReceiver";
        @Override
        public void onReceive(Context context, Intent intent) {
             if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
                Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (null != parcelableExtra) {
                    NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                    switch (networkInfo.getState()) {
                        case CONNECTED:
                            break;
                        case CONNECTING:
                            break;
                        case DISCONNECTED:
                            break;
                        case DISCONNECTING:
                            break;
                        case SUSPENDED:
                            break;
                        case UNKNOWN:
                            break;
                        default:
                            break;
                    }
                }
            }

        }
    }
}

